<?php
namespace App\Query;

use App\Model\MallOrder;
use App\Query\Traits\UserTrait;

class MallOrderQuery extends \HyperfStore\Query
{
    use UserTrait;
    
    // 构建模型
    protected static function build()
    {
        return MallOrder::latest();
    }
    
    // 渴望加载
    protected static function with()
    {
        return ['user'];
    }
    
    // 时间区间
    protected static function queryTime($model, $where)
    {
        return $model->whereBetween('created_at', $where);
    }
    
    // 查询时间段购买的数量
    public static function dayCount($model, $time)
    {
        return static::queryTime($model, [$time, time()])->sum('count');
    }
    
    // 所属商家
    public static function shop($shop)
    {
        return static::model()->where('shop_id', $shop->id);
    }
    
    // 获取未结算的订单
    public static function settleNo($model)
    {
        return $model->where('status_settle', MallOrder::STATUS_SETTLE_NO);
    }
    
    // 已支付的订单
    public static function statusPay($model)
    {
        return $model->where('status', '>', MallOrder::STATUS_POST);
    }
}